package main

import (
	"log"
	"fmt"
	"time"
	"strings"
	"github.com/PuerkitoBio/goquery"
	"./downloader"
	"./scheduler"
	"./spiders"
	// "./model"
)

var (
	StartUrl = "/%s/ch50/g183" // 目标页面
	BaseUrl  = "https://www.dianping.com" // 种子站点
	ReplaceBaseUrl  = "http://www.dianping.com" // 种子站点


	maxPage int = 99
	cars    []spiders.QcCar
)

func Start(url string, ch chan []spiders.QcCar) {
	// fmt.Printf("dingluchao-log,url:%v", url)
	body := downloader.Get(BaseUrl + url)
	// bodys := downloader.Get("http://www.dianping.com/member/1716198236")
	// la,_ := goquery.NewDocumentFromReader(bodys)
	// fmt.Printf("la%v=================>>>", la.Find(".cur").Text());
	
	doc, err := goquery.NewDocumentFromReader(body)
	if err != nil {
		log.Printf("Downloader.Get err: %v", err)
	}

	

	currentPage := spiders.GetCurrentPage(doc)
	nextPageUrl, _ := spiders.GetNextPageUrl(doc)

	// fmt.Printf("result:%v ---end", nextPageUrl);

	if currentPage > 0 && currentPage <= maxPage {
		// log.Println("start.........")
		cars := spiders.GetCars(doc)
		// log.Println(cars)
		ch <- cars
		if url := nextPageUrl; url != "" {
			// fmt.Printf("url:::::::::::%v::::::::::::", url)
			scheduler.AppendUrl(strings.Replace(url, ReplaceBaseUrl, "", -1))
		}

		log.Println(url)
	} else {
		log.Println("Max page !!!")
	}
}

func main() {
	citys := spiders.GetCitys()
	for _, v := range citys {
		// fmt.Printf("result:%v      ", fmt.Sprintf(StartUrl, v.Pinyin));
		if "qingdao" == v.Pinyin {
			scheduler.AppendUrl(fmt.Sprintf(StartUrl, v.Pinyin))
		}
		// break;
	}

	start := time.Now()
	delayTime := time.Second * 60

	ch := make(chan []spiders.QcCar)

L:
	for {
		if url := scheduler.PopUrl(); url != "" {
			go Start(url, ch)
		}

		select {
		case r := <-ch:
			cars = append(cars, r...)
			go Start(scheduler.PopUrl(), ch)
		case <-time.After(delayTime):
			log.Println("Timeout...")
			break L
		}
	}

	if len(cars) > 0 {
		// log.Println("--------------Timeout...")
		// log.Println(cars)
		// model.AddCars(cars)
	}

	log.Printf("Time: %s", time.Since(start) - delayTime)

	time.Sleep(time.Duration(200) * time.Hour)

}
